package com.ding.custommanagersys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ding.custommanagersys.bo.EmailBo;
import com.ding.custommanagersys.entity.Email;
import com.ding.custommanagersys.mapper.EmailMapper;
import com.ding.custommanagersys.mapper.UserMapper;
import com.ding.custommanagersys.service.IEmailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ding
 * @since 2023-04-21
 */
@Service
public class EmailServiceImpl extends ServiceImpl<EmailMapper, Email> implements IEmailService {

    @Autowired
    private EmailMapper emailMapper;

    @Override
    public Integer sendEmail(EmailBo bo) {
        String customId = bo.getCustomId();
        //将id切割
        String[] customIds = customId.split("、");

        Date date=new Date(); //获取一个Date对象
        DateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //创建一个格式化日期对象
        String dayTime= simpleDateFormat.format(date); //格式化后的时间

        int num = 0;
        for (String id : customIds) {
            Email email = new Email();
            email.settAcceptId(id);
            email.settContent(bo.getContent());
            email.settSend("admin");
            email.settCreateTime(dayTime);

            int insert = emailMapper.insert(email);
            num += insert;
        }

        return num;
    }

    @Override
    public Page<Email> getAllEmail(EmailBo bo) {
        Page<Email> page = new Page<>(bo.getPageNo(),bo.getPageSize());
        List<Email> allEmail = emailMapper.getAllEmail(page, bo.getCustomId());
        page.setRecords(allEmail);
        return page;
    }

    @Override
    public Email getOneEmail(String emailId) {
        QueryWrapper<Email> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t_email_id",Integer.parseInt(emailId));
        return getOne(queryWrapper);
    }

    @Override
    public Integer deleteEmail(List<String> ids,String userId) {
        return emailMapper.deleteEmail(ids,userId);
    }
}
